package com.be.beadmin.project.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.InfoAndPage;
import com.be.beadmin.project.dto.input.MasterPlanDto;
import com.be.beadmin.project.dto.input.PlanStageDto;
import com.be.beadmin.project.service.MasterPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author zjm
* @date 2023-01-12
**/
@Api(tags = {"项目管理-项目计划模块"})
@RestController
public class MasterPlanController {

    @Autowired
    private  MasterPlanService masterPlanService;

    @ApiOperation("获取项目计划分页信息")
    @PostMapping(value = "/project/getMasterPlanByPage")
    public ResultModel getMasterPlanByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(masterPlanService.getMasterPlanByPage(pageEntity)) ;
    }

    @ApiOperation("获取项目计划及各阶段分页信息")
    @PostMapping(value = "/project/getMasterPlanAndStageByPage")
    public ResultModel getMasterPlanAndStageByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(masterPlanService.getMasterPlanAndStageByPage(pageEntity)) ;
    }

    @ApiOperation("获取项目id获取各阶段信息")
    @PostMapping(value = "/project/getStageByinfoId")
    public ResultModel getMasterPlanAndStageByPage(@RequestParam String infoId)  {
        return ResultModel.success(masterPlanService.getStagesByInfoId(infoId)) ;
    }

    @ApiOperation("获取项目id获取各阶段信息bybatchPlanId")
    @PostMapping(value = "/project/getStageByBatchPlanId")
    public ResultModel getMasterPlanAndStageByPageByBatchPlanId(@RequestParam String batchPlanId)  {
        return ResultModel.success(masterPlanService.getStagesByInfoBatchPlanId(batchPlanId)) ;
    }


    @ApiOperation("根据项目ID获取项目计划分页信息")
    @PostMapping(value = "/project/getMasterPlanByPageAndInfoId")
    public ResultModel getMasterPlanByPageAndInfoId(@RequestBody InfoAndPage infoAndPage)  {
        return ResultModel.success(masterPlanService.getMasterPlanByPageAndInfoId(infoAndPage)) ;
    }

    @ApiOperation("获取项目主计划各阶段的信息")
    @PostMapping("/project/getMasterPlanStageInfo")
    public ResultModel getMasterPlanStageInfo(){
        return ResultModel.success(masterPlanService.getMasterPlanStageInfo());
    }
    @ApiOperation("添加一个项目主计划")
    @PostMapping("/project/addMasterPlan")
    public ResultModel addMasterPlan(@RequestParam String infoId){
//        try {

            int result = masterPlanService.addMasterPlan(infoId);

            if (result == -1){
                System.out.println("yeah:"+result);
                return ResultModel.error(-1,"该项目已有主计划，请勿重复添加");
            }else if (result == -2) {
                return ResultModel.error(-2,"项目id与项目名称不对应");
            }else if (result == -3) {
                return ResultModel.error(-3,"未查询到柜体信息，请先录入柜体信息");
            }
            return ResultModel.success("项目发布成功");

//        }catch (Exception e){
//            ResultModel.error(-2,e.getMessage());
//        }
    }
    @ApiOperation("删除一个项目主计划")
    @PostMapping("/project/delMasterPlan")
    public ResultModel delMasterPlan(@RequestBody MasterPlanDto masterPlanDto){
        masterPlanService.delMasterPlan(masterPlanDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("修改一个项目主计划")
    @PostMapping("/project/updateMasterPlan")
    public ResultModel updateMasterPlan(@RequestBody MasterPlanDto masterPlanDto){
        try {
            masterPlanService.updateMasterPlan(masterPlanDto);
            return ResultModel.success(ResultModel.UPDATE);

        } catch (Exception b) {
            return ResultModel.error(-1,b.getMessage());
        }

    }

    @ApiOperation("修改一个项目主计划各阶段时间")
    @PostMapping("/project/updateMasterPlanStageTime")
    public ResultModel updateMasterPlanStageTime(@RequestBody List<PlanStageDto> planStageDtos){
        try {
            masterPlanService.updateMasterPlanStageTime(planStageDtos);
            return ResultModel.success(ResultModel.UPDATE);
        }  catch (Exception e) {
            return ResultModel.error(-1,e.getMessage());
        }


    }
}